package com.uhylab.uhuvinetest.activity;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;

import com.uhylab.uhuvinetest.PushService;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.base.UHBaseActivity;
import com.uhylab.uhuvinetest.utils.sharedpreutils.SharedPreUtils;
import com.uhylab.uhuvinetest.utils.toastutil.ToastUtil;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.Calendar;

public class UHManagerActivity extends UHBaseActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private ImageView manager_back;
    private TextView tv_alarm;
    private TextView tv_appaydoctor;
    private Switch switch_alerm;
    private AutoLinearLayout ll_alerm;
    private AutoRelativeLayout iv_alermtime;
    private AutoRelativeLayout iv_alermweek;
    //设置提醒的时间以及时间文本
    private TextView tv_alermtime;

    //设置的周期
    private TextView tv_alermweek;

    private String[] mItems = {"一天","两天","三天","四天","五天"};

    private String time ;
    private int week ;

    private AlarmManager alarmManager;


    @Override
    public int bindLayout() {
        return R.layout.activity_uhmanager;
    }

    @Override
    public void initView(View view) {
        manager_back = (ImageView) findViewById(R.id.manager_back);
        tv_alarm = (TextView) findViewById(R.id.tv_alarm);
        tv_appaydoctor = (TextView) findViewById(R.id.tv_appaydoctor);
        switch_alerm = (Switch) findViewById(R.id.switch_alerm);
        ll_alerm = (AutoLinearLayout) findViewById(R.id.ll_alerm);
        iv_alermtime = (AutoRelativeLayout) findViewById(R.id.seting_time);
        iv_alermweek = (AutoRelativeLayout) findViewById(R.id.iv_alermweek_re);
        tv_alermtime = (TextView) findViewById(R.id.tv_alermtime);
        tv_alermweek = (TextView) findViewById(R.id.tv_alermweek);

        alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);

        time = SharedPreUtils.getString(this,"tv_alermtime","6:20") ;
            tv_alermtime.setText(time);

        week = SharedPreUtils.getInt(this,"tv_alermweek",1) ;
            tv_alermweek.setText(mItems[week]);

    }

    @Override
    public void doBusiness(Context mContext) {
        manager_back.setOnClickListener(this);
        tv_alarm.setOnClickListener(this);
        tv_appaydoctor.setOnClickListener(this);
        switch_alerm.setOnCheckedChangeListener(this);
        iv_alermtime.setOnClickListener(this);
        iv_alermweek.setOnClickListener(this);
        if (switch_alerm.isChecked()) {
            Intent intent = new Intent(this,
                    PushService.class);
            intent.putExtra("time", 24*60);
            startService(intent);
        }

    }

//    private void addAlerm() {
//        Calendar calendar = Calendar.getInstance();
//
//        int minute = Integer.parseInt(time.substring(time.indexOf(":")+1));
//        int hourOfDay = Integer.parseInt(time.substring(0,time.indexOf(":")));
//
//        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
//        calendar.set(Calendar.MINUTE, minute);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
//
//        AlarmData ad = new AlarmData(calendar.getTimeInMillis());
//
//        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
//                ad.getTime(),
//                week*24*60*60*1000,
//                PendingIntent.getBroadcast(getContext(), ad.getId(), new Intent(getContext(), AlarmReceiver.class), 0));
//    }

    @Override
    public void resume() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.manager_back:
                finish();
                break;
            case R.id.tv_alarm:
                break;
            case R.id.tv_appaydoctor:
                ToastUtil.show(this,"即将开通！");
                break;

            case R.id.iv_alermweek_re:
//                AlertDialog.Builder builder = new
                new AlertDialog.Builder(this)
                        .setTitle("设置周期")
                        .setSingleChoiceItems(mItems, week!=-1?week:0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tv_alermweek.setText(mItems[which]);
//                                SharedPreUtils.setString(UHManagerActivity.this,"tv_alermweek",which + "");
                                SharedPreUtils.setInt(UHManagerActivity.this,"tv_alermweek",which);
                            }
                        })
                        .create()
                        .show();
                break;
            case R.id.seting_time:
                new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener(){

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        tv_alermtime.setText(hourOfDay + ":" + minute);
                        SharedPreUtils.setString(UHManagerActivity.this,"tv_alermtime",hourOfDay + ":" + minute);
                    }
                },6,20,true).show();

                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            ll_alerm.setVisibility(View.VISIBLE);
        } else {
            ll_alerm.setVisibility(View.INVISIBLE);
        }
    }

    private static class AlarmData{
        public AlarmData(long time) {
            this.time = time;

            date = Calendar.getInstance();
            date.setTimeInMillis(time);

            timeLabel = String.format("%d月%d日 %d:%d",
                    date.get(Calendar.MONTH)+1,
                    date.get(Calendar.DAY_OF_MONTH),
                    date.get(Calendar.HOUR_OF_DAY),
                    date.get(Calendar.MINUTE));
        }

        public long getTime() {
            return time;
        }

        public String getTimeLabel() {
            return timeLabel;
        }

        @Override
        public String toString() {
            return getTimeLabel();
        }

        public int getId(){
            return (int)(getTime()/1000/60);
        }

        private String timeLabel="";
        private long time = 0;
        private Calendar date;
    }

}
